
<*
* 发票管理
* invoiceManage
* edit by ZHANGKE
*>

<template>
    <div class="invoice-manage-section">
        <!--发票管理 invoiceManage -->
        <el-row>
            <el-col :span="24">
                <el-tabs v-model="activeName" type="card" @tab-click="handleTabClick">
                    <el-tab-pane label="可开发票" name="invoice">
                        <el-form :inline="true" :model="iForm" class="demo-form-inline" size="mini">
                            <el-form-item label="订单编号/标题：">
                                <el-input v-model="iForm.keyword" size="mini" clearable></el-input>
                            </el-form-item>
                            <el-form-item label="订单时间：" class="invoice-manage-item">
                                <el-date-picker
                                        size="mini"
                                        v-model="iForm.sDate"
                                        type="daterange"
                                        range-separator="-"
                                        start-placeholder="开始日期"
                                        end-placeholder="结束日期"
                                        value-format="timestamp"
                                ></el-date-picker>
                            </el-form-item>

                            <el-form-item label="订单金额：">
                                <el-input-number class="order-jinE-number" v-model="iForm.startFinalPrice" size="mini" controls-position="right" @change="handleJinEChange" :min="0" :max="100000"></el-input-number>
                                -
                                <el-input-number class="order-jinE-number" v-model="iForm.endFinalPrice" size="mini" controls-position="right" @change="handleJinEChange" :min="0" :max="100000"></el-input-number>

                            </el-form-item>
                            <el-form-item>
                                <el-button size="mini" type="primary" @click="searchInvoice">查询</el-button>
                            </el-form-item>
                        </el-form>
                        <el-form class="demo-form-inline" size="mini">
                            <el-form-item label="">
                                <el-button type="success" @click="batchInvoicing">批量开票</el-button>
                            </el-form-item>
                        </el-form>
                        <el-table
                                :data="tableInvoiceData"
                                border
                                @selection-change="handleSelectionChange"
                                style="width: 100%">
                            <el-table-column
                                    type="selection"
                                    width="55">
                            </el-table-column>
                            <el-table-column
                                    prop="orderNo"
                                    label="订单编号"
                                    width="140">
                            </el-table-column>
                            <el-table-column
                                    prop="orderTimeMy"
                                    label="订单时间"
                                    width="100">
                            </el-table-column>
                            <el-table-column
                                    prop="title"
                                    label="订单标题"
                                    width="120">
                            </el-table-column>
                            <el-table-column
                                    prop="type"
                                    label="类型（账单/其他）"
                                    width="">
                            </el-table-column>
                            <el-table-column
                                    prop="finalPrice"
                                    label="订单实付金额"
                                    width="">
                            </el-table-column>
                            <el-table-column
                                    prop="invoicePrice"
                                    label="可开发票金额"
                                    width="180">
                            </el-table-column>
                        </el-table>

                        <el-pagination
                                class="pagination-part"
                                background
                                @current-change="changePageAll"
                                :current-page.sync="currentPageAll"
                                :page-size="pageSizeAll"
                                layout="prev, pager, next"
                                :total="personListTotalAll">
                        </el-pagination>

                    </el-tab-pane>
                    <el-tab-pane label="发票清单" name="list">
                        <el-form :inline="true" :model="lForm" class="demo-form-inline">
                            <el-form-item label="开票时间：">
                                <el-date-picker
                                        size="mini"
                                        v-model="lForm.sDate"
                                        type="daterange"
                                        range-separator="-"
                                        start-placeholder="开始日期"
                                        end-placeholder="结束日期"
                                        value-format="timestamp"
                                ></el-date-picker>
                            </el-form-item>

                            <el-form-item label="发票状态：">
                                <el-select size="mini" v-model="lForm.status" placeholder="请选择" clearable value="">
                                    <el-option
                                            v-for="item in orderStatus"
                                            :key="item.code"
                                            :label="item.label"
                                            :value="item.code"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item>
                                <el-button type="primary" @click="searchList">查询</el-button>
                            </el-form-item>

                        </el-form>
                        <el-table
                                :data="tableListData"
                                border
                                style="width: 100%">

                            <el-table-column
                                    prop="createTimeMy"
                                    label="申请时间"
                                    width="140">
                            </el-table-column>
                            <el-table-column
                                    prop="title"
                                    label="发票抬头"
                                    width="140">
                            </el-table-column>

                            <el-table-column
                                    prop="total"
                                    label="发票总额"
                                    width="150">
                            </el-table-column>
                            <el-table-column
                                    prop="type"
                                    label="发票性质"
                                    width="150">
                            </el-table-column>
                            <el-table-column
                                    prop="statusMy"
                                    label="发票状态"
                                    width="150">
                            </el-table-column>
                            <el-table-column
                                    prop="remark"
                                    label="备注"
                                    width="150">
                            </el-table-column>
                            <el-table-column
                                    fixed="right"
                                    label="操作"
                                    width="180">
                                <template slot-scope="scope">
                                    <el-button @click="handleRefundClick(scope.row)" type="text" size="small">退票</el-button>
                                    <el-button @click="handleDetailClick(scope.row)" type="text" size="small">详情</el-button>
                                    <el-button @click="handleExportClick(scope.row)" type="text" size="small">导出明细</el-button>
                                </template>
                            </el-table-column>
                        </el-table>


                    </el-tab-pane>
                </el-tabs>
            </el-col>
        </el-row>



        <modal :visible="invoiceDialogVisible" title="发票开具" width="650" @close="invoiceDialogVisible = false">

            <modal :visible="mainstayDialogVisible" :title="(isInvoiceMainstayNew?'新增':'编辑') +'开票主体'" width="650" @close="mainstayDialogVisible = false">
                <el-form ref="form" :model="mForm" label-width="110px" size="mini">
                    <el-form-item label="发票抬头">
                        <el-input v-model="mForm.title" ></el-input>
                    </el-form-item>
                    <el-form-item label="开具类型">
                        企业
                    </el-form-item>
                    <el-form-item label="发票类型">
                        <el-radio-group v-model="mForm.invoiceType">
                            <el-radio-button label="企业增值税普通发票"></el-radio-button>
                            <el-radio-button label="组织(非企业)增值税普通发票"></el-radio-button>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="税务登记证号">
                        <el-input v-model="mForm.taxRegistrationAccount" ></el-input>
                    </el-form-item>
                    <el-form-item label="开户银行名称">
                        <el-select v-model="mForm.bank" placeholder="请选择"  value="" clearable>
                            <el-option :label="bank.label" :value="bank.bankId" v-for="bank in bankItems" :key="bank.bankId"></el-option>

                        </el-select>
                    </el-form-item>
                    <el-form-item label="基本开户账号">
                        <el-input v-model="mForm.bankNo" ></el-input>
                    </el-form-item>
                    <el-form-item label="注册场所地址">
                        <el-input v-model="mForm.address"></el-input>
                    </el-form-item>
                    <el-form-item label="注册固定电话">
                        <el-input v-model="mForm.phone" ></el-input>
                    </el-form-item>
                </el-form>

                <div class="content-footer">
                    <el-button @click="mainstayDialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="mainstaySubmitDialog">确定</el-button>
                </div>
            </modal>
            <modal :visible="addressDialogVisible" :title="(isInvoiceAddressNew?'新增':'编辑') +'寄送地址'" width="650" @close="addressDialogVisible = false">
                <el-form  ref="form" :rules="aFormRules" :model="aForm" label-width="110px" size="mini" class="demo-form-inline">
                    <el-form-item label="收件人姓名：" prop="name">
                        <el-input  v-model="aForm.name"  clearable></el-input>
                    </el-form-item>
                    <el-form-item label="所在地区：" prop="addressChose">
                        <div class="block">
                            <el-cascader
                                    v-model="aForm.addressChose"
                                    :options="cascaderOptions"
                                    :props="{ checkStrictly: true,value:'name',label:'name' }"
                                    size="mini"
                                    clearable></el-cascader>
                        </div>
                    </el-form-item>
                    <el-form-item label="街道地址：" prop="address">
                        <el-input v-model="aForm.address" ></el-input>
                    </el-form-item>
                    <el-form-item label="邮政编码：" prop="mailCode">
                        <el-input v-model="aForm.mailCode" ></el-input>
                    </el-form-item>
                    <el-form-item label="手机号：" prop="phone">
                        <el-input v-model="aForm.phone"></el-input>
                    </el-form-item>
                    <el-form-item >
                        <!--<el-checkbox v-model="aForm.defaulted" true-label="1" false-label="0">设为默认地址</el-checkbox>-->
                        <el-checkbox v-model="aForm.defaultedMy">设为默认地址</el-checkbox>
                    </el-form-item>
                </el-form>
                <div  class="content-footer">
                    <el-button @click="addressDialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="addressSubmitDialog">确定</el-button>
                </div>
            </modal>

            <el-row>
                <el-col class="title-container">
<!--                    <el-alert-->
<!--                            :title="'您选取了'+  innerInvoiceNumber +'条单据开具发票 （若选中多条订单，订单金额将合并开具在一张票据中）'"-->
<!--                            type="error"-->
<!--                           >-->
<!--                    </el-alert>-->
<!--                    <el-alert-->
<!--                            :title="'开票金额'+nForm.invoiceTotal"-->
<!--                            type="error"-->
<!--                            >-->
<!--                    </el-alert>-->
<!--                    <el-alert-->
<!--                            :title="'您选取了'+  innerInvoiceNumber +'条单据开具发票 （若选中多条订单，订单金额将合并开具在一张票据中）'"-->
<!--                            type="success"-->
<!--                            show-icon>-->
<!--                    </el-alert>-->
<!--                    <el-alert-->
<!--                            :title="'开票金额'+nForm.invoiceTotal"-->
<!--                            type="success"-->
<!--                            show-icon>-->
<!--                    </el-alert>-->
<!--                    <el-alert-->
<!--                            :title="'您选取了'+  innerInvoiceNumber +'条单据开具发票 （若选中多条订单，订单金额将合并开具在一张票据中）'"-->
<!--                            type="warning"-->
<!--                            description=""-->
<!--                            show-icon>-->
<!--                    </el-alert>-->

                    <div class="alert-strong-title">您选取了 <strong> {{innerInvoiceNumber}}</strong> 条单据开具发票 （若选中多条订单，订单金额将合并开具在一张票据中）</div>
                    <div class="alert-strong-title">开票金额: <strong>￥ {{nForm.invoiceTotal}}</strong></div>
<!--                    <span>您选取了  {{innerInvoiceNumber}} 条单据开具发票 （若选中多条订单，订单金额将合并开具在一张票据中）</span>-->
<!--                    <span>开票金额 {{ nForm.invoiceTotal}}</span>-->
                </el-col>
            </el-row>

            <el-form :model="nForm">

                <el-form-item label="" :label-width="formLabelWidth">
                    <el-button type="primary" @click="createInvoiceMainstay">新增开票主体</el-button>
                </el-form-item>
                <el-form-item label="" :label-width="formLabelWidth">
                    <el-table
                            :data="tableMainstayData"
                            border
                            size="mini"
                            style="width: 100%">
                        <el-table-column
                                label="发票抬头"
                                width="280">
                            <template slot-scope="scope">
                                <el-radio v-model="invoiceRadio" @change="invoiceRadioChange(scope.row)" :label="scope.row.title"></el-radio>
                            </template>
                        </el-table-column>

                        <el-table-column
                                prop="taxRegistrationAccount"
                                label="税务登记证号"
                                width="">
                        </el-table-column>
                        <el-table-column
                                fixed="right"
                                label="操作"
                                width="100">
                            <template slot-scope="scope">
                                <el-button @click="editInvoiceMainstay(scope.row)" type="text" size="small">修改</el-button>
                                <el-button @click="removeInvoiceMainstay(scope.row)" type="text" size="small">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-form-item>
                <el-form-item label="" :label-width="formLabelWidth">

                    <div class="invoice-detail-wrap">
                        <div class="invoice-detail-left">
                            <div><span>发票抬头：</span></div>
                            <div><span>开具类型：</span></div>
                            <div><span>发票类型：</span></div>
                            <div><span>税务登记证号：</span></div>
                            <div><span>开户银行名称：</span></div>
                            <div><span>基本开户账号：</span></div>
                            <div><span>注册场所地址：</span></div>
                            <div><span>注册固定电话：</span></div>
                        </div>
                        <div class="invoice-detail-right">
                            <div><span>{{nForm.detail.title}}</span></div>
                            <div><span>{{nForm.detail.companyType}}</span></div>
                            <div><span>{{nForm.detail.invoiceType}}</span></div>
                            <div><span>{{nForm.detail.taxRegistrationAccount}}</span></div>
                            <div><span>{{nForm.detail.bank}}</span></div>
                            <div><span>{{nForm.detail.bankNo}}</span></div>
                            <div><span>{{nForm.detail.address}}</span></div>
                            <div><span>{{nForm.detail.phone}}</span></div>
                        </div>
                    </div>

                </el-form-item>
                <el-form-item label="" :label-width="formLabelWidth">
                    <el-input
                            type="textarea"
                            :autosize="{ minRows: 1, maxRows: 4}"
                            placeholder="该备注信息会展示在发票上"
                            clearable
                            v-model="nForm.textarea">
                    </el-input>
                </el-form-item>
                <el-form-item label="" :label-width="formLabelWidth">
                    <el-button type="primary" @click="createInvoiceAddress">新增地址</el-button>
                </el-form-item>
                <el-form-item label="" :label-width="formLabelWidth">
                    <el-table
                            :data="tableAddressData"
                            border
                            size="mini"
                            style="width: 100%">
                        <el-table-column
                                label=""
                                width="55">
                            <template slot-scope="scope">
                                <el-radio v-model="addressRadio" @change="addressRadioChange(scope.row)" :label="scope.row.id">&nbsp;</el-radio>
                            </template>
                        </el-table-column>

                        <el-table-column
                                prop="name"
                                label="收件人姓名"
                                width="90">
                        </el-table-column>
                        <el-table-column
                                prop="phone"
                                label="电话"
                                width="100">
                        </el-table-column>
                        <el-table-column
                                prop="addressMy"
                                label="地址"
                                width="">
                        </el-table-column>
                        <el-table-column
                                prop="mailCode"
                                label="邮编"
                                width="70">
                        </el-table-column>
                        <el-table-column
                                fixed="right"
                                label="操作"
                                width="90">
                            <template slot-scope="scope">
                                <el-button @click="editInvoiceAddress(scope.row)" type="text" size="small">修改</el-button>
                                <el-button @click="removeInvoiceAddress(scope.row)" type="text" size="small">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-form-item>
            </el-form>
            <div  class="content-footer">
                <el-button @click="invoiceDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="invoiceSubmitDialog">开票</el-button>
            </div>
        </modal>


    </div>
</template>

<script>
    import { cityList } from '../../../assets/data/city';
    import moment from 'moment';
    import * as _ from 'lodash';
    export default {
        name: 'invoiceManage',
        data() {
            return {
                activeName: 'invoice',
                orderStatus:[{label:'开票中',code:1},{label:'已开票',code:2}],
                iForm:{},
                lForm:{},
                mForm:{},
                aForm:{},
                aFormRules:{
                    name: [
                        { required: true, message: '请输入名称', trigger: 'blur' },
                        { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                    ],
                    region: [
                        { required: true, message: '请选择区域', trigger: 'change' }
                    ],
                    address: [
                        { required: true, message: '请填写地址', trigger: 'blur' }
                    ],
                    telephone: [
                        { required: true, message: '请输入手机号码', trigger: 'blur' },
                        { pattern: /^[1][345678][0-9]{9}$/, message: '手机号格式不正确', trigger: 'change' },
                    ],
                    postCode: [
                        { required: true, message: '请邮政编码', trigger: 'blur' }
                    ]
                },
                tableInvoiceData: [
                    {
                        id:'503200',
                        profile: '--',
                        name: '小明',
                        code: 'Is0fLdo3WCv',
                        prizeName:'--',
                        prize:'3366',
                        isGot:'已领取',
                        invoicePrice:'2000',
                        mType:'企业增值税普通发票',
                        oPerson:'张三',
                        remark:'--'
                    },
                    {
                        id:'503201',
                        profile: '--',
                        name: '小明',
                        code: 'Is0fLdo3WCv',
                        prizeName:'--',
                        prize:'1133',
                        isGot:'已领取',
                        invoicePrice:'4008',
                        mType:'企业增值税普通发票',
                        oPerson:'张三',
                        remark:'--'
                    },
                    {
                        id:'503202',
                        profile: '--',
                        name: '小明',
                        code: 'Is0fLdo3WCv',
                        prizeName:'--',
                        prize:'7766',
                        isGot:'已领取',
                        invoicePrice:'14422',
                        mType:'企业增值税普通发票',
                        oPerson:'张三',
                        remark:'--'
                    },
                    {
                        id:'503203',
                        profile: '--',
                        name: '小明',
                        code: 'Is0fLdo3WCv',
                        prizeName:'--',
                        prize:'30000',
                        isGot:'已领取',
                        invoicePrice:'82004',
                        mType:'企业增值税普通发票',
                        oPerson:'张三',
                        remark:'--'
                    }],
                tableListData:[{creatDate:'2019-07-23',name:'上海东方商务有限公司',profile:'200.94',loginData:'--',prize:'已开票',remark:'--'},
                    {creatDate:'2019-07-25',name:'上海东方商务有限公司',profile:'200.94',loginData:'--',prize:'已开票',remark:'--'},
                    {creatDate:'2019-07-21',name:'上海东方商务有限公司',profile:'550.02',loginData:'--',prize:'正在开票',remark:'--'}],
                tableMainstayData: [
                    {
                        id:'100300',
                        countNo: '22665',
                        name: '上海东方红有限公司',
                        index: '1',
                        prizeName:'--',
                        prize:'笔记本',
                        type:'企业',
                        radioType:'企业增值税普通发票',
                        faxID:'910432888542',
                        address:'上海市衡山路100号',
                        telephone:'13899994444',
                        bank:'浙商银行'
                    },
                    {
                        id:'100301',
                        countNo: '35342',
                        name: '上海昆仑山有限责任公司',
                        index: '2',
                        prizeName:'--',
                        prize:'笔记本',
                        type:'企业',
                        radioType:'企业增值税普通发票',
                        faxID:'910432888542',
                        address:'上海市湖南路200号',
                        telephone:'13822221111',
                        bank:'北京银行'
                    },
                    {
                        id:'100302',
                        countNo: '2224656',
                        name: '中国远洋航运集团公司',
                        index: '3',
                        prizeName:'--',
                        prize:'笔记本',
                        type:'企业',
                        radioType:'企业增值税普通发票',
                        faxID:'910432888542',
                        address:'上海市天山路1078号',
                        telephone:'1380004444',
                        bank:'南京银行'
                    },
                    {
                        id:'100303',
                        countNo: '56546456',
                        name: '个人',
                        index: '4',
                        prizeName:'--',
                        prize:'笔记本',
                        type:'个人',
                        radioType:'增值税普通发票',
                        faxID:'910432888542',
                        address:'上海市南京东路78号',
                        telephone:'13355551111',
                        bank:'宁波银行'
                    }],
                tableAddressData:[{id:'00140',postCode:'100302',name:'张三',telephone:'13900002222',address:'上海浦东新区银城东路188号'},
                    {id:'00141',postCode:'100302',name:'张三',telephone:'13900002222',address:'上海浦东新区银城东路188号'},
                    {id:'00142',postCode:'100302',name:'张三',telephone:'13900002222',address:'上海浦东新区银城东路188号'}],
                invoiceDialogVisible:false,
                mainstayDialogVisible:false,
                addressDialogVisible:false,
                nForm:{detail:{}},
                formLabelWidth:'6px',
                innerInvoiceNumber:5,
                invoiceTotal: "￥330.2",
                invoiceRadio: null,

                isInvoiceMainstayNew:null,
                isInvoiceAddressNew:null,
                bankItems:[{bankId:'1104',label:'上海银行'},
                    {bankId:'1105',label:'交通银行'},
                    {bankId:'1106',label:'光大银行'},
                    {bankId:'1107',label:'华夏银行'}],
                cascaderOptions: cityList,
                multipleSelection:[],
              //  detail:{},
                invoiceDialogVisible1:null,
                addressRadio:null,
                currentPageAll:1,
                pageSizeAll:10,
                personListTotalAll:100,
                isDefaultedList:{
                    'false':0,
                    'true':1
                }
            };
        },
        mounted(){
            this.searchInvoice();
            this.searchList();
            let city = localStorage.getItem('city');

            this.cascaderOptions = JSON.parse(city)
        },
        methods: {
            indexMethod(index) {
                return index + 1 ;
            },
            handleTabClick(){

            },
            changePageAll(i){
                this.currentPageAll = i;
                this.searchInvoice()
            },
            searchInvoice(){
                let startDate = this.iForm.sDate? this.iForm.sDate[0]:'',
                    endDate = this.iForm.sDate? this.iForm.sDate[1]:'';
                let param = {
                    keyword:this.iForm.keyword,
                    startDate:startDate,
                    endStart:endDate,
                    startFinalPrice:this.iForm.startFinalPrice,
                    endFinalPrice:this.iForm.endFinalPrice,
                    type:'',
                    index:this.currentPageAll
                };
                this.searchOrderInvoice(param)


            },
            searchList(){
                let startDate = this.lForm.sDate? this.lForm.sDate[0]:'',
                    endDate = this.lForm.sDate? this.lForm.sDate[1]:'';
                let data = {
                    startDate:startDate,
                    endDate:endDate,
                    status:this.lForm.status
                };
                this.searchInvoiceList(data)

            },
            getNowFormatDate() {
                //获取当前时间，格式YYYY-MM-DD
                let date = new Date(),
                    seperator = "-",
                    year = date.getFullYear(),
                    month = date.getMonth() + 1,
                    strDate = date.getDate();
                if (month >= 1 && month <= 9) {
                    month = "0" + month;
                }
                if (strDate >= 0 && strDate <= 9) {
                    strDate = "0" + strDate;
                }
                return year + seperator + month + seperator + strDate;
            },
            handleJinEChange(){},
            batchInvoicing(){
                //批量开票
                if ( this.multipleSelection && this.multipleSelection.length){
                    this.initInvoiceDialog(this.multipleSelection);
                    this.invoiceDialogVisible = true;
                    this.searchInvoiceMainstay();
                    this.searchInvoiceAddress()
                }

            },
            handleSelectionChange(val){
                this.multipleSelection = val;
            },
            initInvoiceDialog(list){
                this.nForm.ordersNos = list.map(item=>{
                    return item.orderNo
                });
                this.innerInvoiceNumber = list.length;
                this.nForm.invoiceTotal = list.concat({invoicePrice:0}).reduce(function(total, item ) {
                    return ( _.isNumber(total.invoicePrice)?(+total.invoicePrice): total)+ (+item.invoicePrice) ;
                })
            },
            handleRefundClick(){

            },
            handleDetailClick(){},
            handleExportClick(){},
            invoiceSubmitDialog(){
                //开票提交
             if(this.nForm.detail.id && this.nForm.address){

                 let data = {
                     title:this.nForm.detail.title,//发票抬头
                     total:this.nForm.invoiceTotal||0,//发票总额
                     type:this.nForm.detail.invoiceType,//发票性质
                     ordersNos:this.nForm.ordersNos.join(','),//订单号集合，多个订单号逗号分割
                     remark:this.nForm.textarea,//备注
                     mainId:this.nForm.detail.id, //发票主体ID
                     address_id:this.nForm.address.addressChose //收货地址
                 };
                 this.invoiceCreate(data);
                 this.invoiceDialogVisible = false;
             }else{
                 this.$message({
                     type: 'warning',
                     message: '请选择发票主体和发票收件地址!'
                 });
             }

            },
            createInvoiceMainstay(){
                // 新增开票主体
                this.mainstayDialogVisible = true;
                this.isInvoiceMainstayNew  = true;
                this.mForm = {}
            },
            editInvoiceMainstay(row){
                // 编辑开票主体
                this.mainstayDialogVisible = true;
                this.isInvoiceMainstayNew  = false;
                this.mForm =  _.cloneDeep(row);
            },
            removeInvoiceMainstay(row){
                //删除开票主体
                //this.tableMainstayData = this.tableMainstayData.filter(item => item.id !== row .id)

                this.$confirm('确定执行此删除操作?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteInvoiceMainstay({id:row.id})
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            mainstaySubmitDialog(){
                //新增和编辑开票主体 提交
                let data = {
                    id:this.mForm.id,
                    title: this.mForm.title,
                    companyType: '企业'||this.mForm.companyType,
                    taxRegistrationAccount:this.mForm.taxRegistrationAccount,
                    invoiceType:this.mForm.invoiceType,
                    bankNo:this.mForm.bankNo,
                    address:this.mForm.address,
                    bank:this.mForm.bank,
                    phone:this.mForm.phone
                };
                //this.tableMainstayData.push(data);
                this.mForm = {};
                this.mainstayDialogVisible = false;
                this.isInvoiceMainstayNew ? this.addInvoiceMainstay(data): this.updateInvoiceMainstay(data);

            },
            createInvoiceAddress(){
                // 新增地址dialog
                this.addressDialogVisible = true;
                this.isInvoiceAddressNew  = true;
                this.aForm = {};
            },
            editInvoiceAddress(row){
                // 编辑地址
                this.addressDialogVisible = true;
                this.isInvoiceAddressNew  = false;
                this.aForm = _.cloneDeep(row);
                this.aForm.addressChose = row.addressChose.split(',') ;
            },
            removeInvoiceAddress(row){
                //删除地址
                //this.tableAddressData = this.tableAddressData.filter(item => item.id !== row .id)

                this.$confirm('确定执行此删除操作?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteInvoiceAddress({id:row.id})
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            addressSubmitDialog(){
                //新增编辑地址 提交
                // let ids = this.tableAddressData[this.tableAddressData.length-1].id;
                let data = {
                    id:this.aForm.id,
                    name: this.aForm.name,
                    mailCode:this.aForm.mailCode,
                    addressChose:this.aForm.addressChose.join(','),
                    defaulted:this.aForm.defaultedMy?'1':'0',
                    address:this.aForm.address,
                    phone:this.aForm.phone,
                }
                //this.tableAddressData.push( data);
                this.isInvoiceAddressNew ? this.addInvoiceAddress(data) : this.updateInvoiceAddress(data)
                this.addressDialogVisible  = false;
                this.aForm = {};
            },
            invoiceRadioChange(row){
                //点击发票主体
                this.nForm.detail =  _.cloneDeep(row);
            },
            addressRadioChange(row){
                //点击发票地址
                this.nForm.address =  _.cloneDeep(row);
            },
            searchOrderInvoice(param){
                //可开票订单列表查询接口
                this.$api.post('/order/invoice', param, success => {
                    if(success.result){
                        this.personListTotalAll = success.totalCount;
                        this.currentPageAll = success.pageIndex;
                        this.tableInvoiceData = success.result.map(item =>{
                            if(item.orderTime) {
                                item.orderTimeMy = moment(item.orderTime).format("YYYY-MM-DD")
                            }
                            return item
                        })
                    }
                }, fal => {
                    this.$message.error(fal);
                });
            },
            searchInvoiceMainstay(){
                //查询发票主体
                this.$api.get('/invoice/main/list',null, success => {
                    if(success){
                        this.tableMainstayData = success||[]
                    }
                }, fal => {
                    this.$message.error(fal);
                });
            },
            addInvoiceMainstay(param){
                //新增发票主体
                this.$api.post('/invoice/main',param, success => {
                    this.searchInvoiceMainstay()
                }, fal => {
                    this.$message.error(fal);
                });
            },
            updateInvoiceMainstay(param){
                //更新修改发票主体
                this.$api.post('/invoice/main/update',param, success => {
                    this.searchInvoiceMainstay()
                    this.$message({
                        type: 'success',
                        message: '操作成功!'
                    });
                }, fal => {
                    this.$message.error(fal);
                });
            },
            deleteInvoiceMainstay(param){
                //删除发票主体
                this.$api.get('/invoice/main/del',param, success => {
                    this.searchInvoiceMainstay()
                    this.$message({
                        type: 'success',
                        message: '操作成功!'
                    });
                }, fal => {
                    this.$message.error(fal);
                });
            },
            searchInvoiceAddress(){
                //查询发票地址
                this.$api.get('/delivery/list',null, success => {
                    if(success){
                        // this.tableAddressData = success||[]
                        if (Array.isArray(success)) {
                            this.tableAddressData = success.map(item => {
                                item.defaultedMy = !!item.defaulted;
                                if(item.address){
                                    item.addressMy = item.addressChose && item.addressChose.split(',').join('') + item.address
                                }
                                return item
                            })
                        }
                    }
                }, fal => {
                    this.$message.error(fal);
                });
            },
            addInvoiceAddress(param){
                //新增发票地址
                this.$api.post('/delivery/add',param, success => {
                    this.searchInvoiceAddress()
                    this.$message({
                        type: 'success',
                        message: '操作成功!'
                    });
                }, fal => {
                    this.$message.error(fal);
                });
            },
            deleteInvoiceAddress(param){
                //删除发票地址
                this.$api.get('/delivery/del',param, success => {
                    this.searchInvoiceAddress()
                    this.$message({
                        type: 'success',
                        message: '操作成功!'
                    });
                }, fal => {
                    this.$message.error(fal);
                });
            },
            invoiceCreate(param){
                //申请开票
                this.$api.post('/invoice/create',param, success => {
                    this.searchInvoice()
                    this.$message({
                        type: 'success',
                        message: '操作成功!'
                    });
                }, fal => {
                    this.$message.error(fal);
                });
            },
            searchInvoiceList(param){
                //发票清单查询
                this.$api.post('/invoice',param, success => {
                    if (success){
                        let that = this
                        this.tableListData = success.map(item =>{
                            if(item.createTime) {
                                item.createTimeMy = moment(item.createTime).format("YYYY-MM-DD")
                            }
                            if (item.status !== undefined ||item.status !== null){
                                item.statusMy = that.orderStatus.find(state=> state.code === item.status).label
                            }
                            return item
                        })
                    }
                }, fal => {
                    this.$message.error(fal);
                });
            },
            updateInvoiceAddress(param){
                //修改地址
                this.$api.post('/delivery/update',param, success => {
                    this.searchInvoiceAddress()
                }, fal => {
                    this.$message.error(fal);
                });
            }
        },
    };
</script>

<style lang="scss" scoped>
    .invoice-manage-section{
        background-color: #fff;
        margin: 20px !important;
        border-radius: 20px;
        padding: 10px 20px;
        width: 100%;
        height: 100%;
        overflow: auto;
        .el-row {
            width: 100%;

            .order-jinE-number{
                width:100px;
            }
            .invoice-manage-item{
                margin-right: 0;
                .el-date-editor--daterange.el-input__inner{
                    width: 220px;
                }
            }
        }
        .invoice-detail-wrap{
            display: flex;
            .invoice-detail-left{width:40%}
            .invoice-detail-left div{text-align: right;}
        }
        .pagination-part{
            text-align:center;
        }

        .content-footer {
            text-align: center;
            margin-top: 20px;
            .el-button {
                padding-left: 30px;
                padding-right: 30px;
            }
        }



        .alert-strong-title{
            strong{
                color: red;font-size: 32px;
            }
        }



        .title-container{
            border: 1px solid #FF9800;
            padding: 10px;
            box-shadow: 0 0 2px rgba(0, 0, 0, 0.2);
            font-size: 14px;
            margin-bottom: 10px;
        }
    }




</style>